/**jquery自动化执行代码*/
$(function(){
	/**服务器session 通过心跳保持登录状态，心跳时间间隔是20分钟*/
    heartFun(true);
    //heartFun(false);
});

/**用户信息*/
function initUserInfo(callback){
	$.ajax({
		url:'hxclub/member/permissions.do',
		type:'get',
		dataType:'json',
		success:function(d){
			if(d.success){
			} 
			callback(d);
		}
	});
}
/**
 * 获取本地存储的用户信息
 * {"success":true,"statusCode":null,"data":{"id":"4028800a50dcdcf30150dce147b00001","realname":"吴攀","email"
	:"abc@qq.com","mphone":"13512430000","checkstatus":"20","levelclass":"1","isDelete":"0","jointype"
	:"2","idcord":"420982199107173123","companyname":"北京鸿鑫","job":"Java","joinDate":"2016-03-28 14:18:13.0","userType":"f"},"attribute":null}
 *
	userInfo = {code:code,pwd:pwd,realname:d.data.realname,mphone:d.data.mphone,email:d.data.email,permissions:[],roles:[],_loginStatus:false,_loginStatus:true};
 * @returns
 */
function getLocalUserInfo(){
	if(window.localStorage){
		var userInfoStr = window.localStorage.getItem('userInfo');
		if(userInfoStr){
			var userInfo = JSON.parse(userInfoStr);
			return userInfo
		}
	} 
	return null;
}

function heartFun(isAsyn,callback){
	$.ajax({
		url:'hxclub/sys/heart.do',
		type:'get',
		async:isAsyn,
		dataType:'json',
		success:function(d){
			if(d.success){
				/*用户已经登录*/
				//console.log('main.js --> d.data.realname = '+d.data.realname);
				$("#userRealName").html(d.data.realname);
				$(".XLogin").removeClass("xCentShow");
				$(".XCent").addClass("xCentShow");

				if(window.localStorage){
					var oldUserInfo = getLocalUserInfo();
					if(oldUserInfo){
						var defaultUserInfo = {realname:d.data.realname,mphone:d.data.mphone,email:d.data.email,permissions:[],roles:[],_loginStatus:true,_loginStatus:true};
//						var defaultUserInfo = {realname:d.data.realname,mphone:d.data.mphone,email:d.data.email,permissions:[],roles:[],ulist:d.data.ulist,_loginStatus:true,_loginStatus:true};
						var userInfo = $.fn.extend({},oldUserInfo, defaultUserInfo);
						initUserInfo(function(data){
							userInfo.permissions = data.data.permissions;
							userInfo.roles = data.data.roles;
							var userInfoStr = JSON.stringify(userInfo); 
							window.localStorage.setItem('userInfo',userInfoStr);
							if(typeof callback == 'function'){
								callback();
							}
						});
						
					} else {
						var userInfo = {realname:d.data.realname,mphone:d.data.mphone,email:d.data.email,permissions:[],roles:[],_loginStatus:true,_loginStatus:true};
						initUserInfo(function(data){
							userInfo.permissions = data.data.permissions;
							userInfo.roles = data.data.roles;
							var userInfoStr = JSON.stringify(userInfo); 
							window.localStorage.setItem('userInfo',userInfoStr);
							if(typeof callback == 'function'){
								callback();
							}
						});
						
					}
					
				}
			} else {
				/*用户未登录*/
                $(".XCent").removeClass("xCentShow");
				$(".XLogin").addClass("xCentShow");
				
				//如果服务器离线，这里自动登录
				/*
				var userInfo = getLocalUserInfo();
				if(userInfo){
					var code = userInfo.code;
					var pwd = userInfo.pwd;
					$.ajax({
	    				url:'hxclub/authen/login.do',
	    				type:'post',
	    				data:{code:code,pwd:pwd},
	    				dataType:'json',
	    				success:function(d){
	    					if(d.success){
	    						$("#userRealName").html(d.data.realname);
	    						$(".XLogin").removeClass("xCentShow");
	    						$(".XCent").addClass("xCentShow");
	    						
	    						if(window.localStorage){
	    							var userInfo = {realname:d.data.realname,mphone:d.data.mphone,email:d.data.email,permissions:[],roles:[],_loginStatus:true,_loginStatus:true};
	    							initUserInfo(function(data){
	    								userInfo.permissions = data.data.permissions;
	    								userInfo.roles = data.data.roles;
	    								var userInfoStr = JSON.stringify(userInfo); 
	    								window.localStorage.setItem('userInfo',userInfoStr);
	    								if(typeof callback == 'function'){
		    								callback();
		    							}
	    							});
	    							
	    						}
	    					} else {
	    						userInfo._loginStatus = false;
	    						var userInfoStr = JSON.stringify(userInfo); 
	    						window.localStorage.setItem('userInfo',userInfoStr);
	    						if(typeof callback == 'function'){
    								callback();
    							}
	    					}
	    				}
	    			});
				}
				*/
			}
			if(isAsyn){//只有异步情况才开启定时心跳
				setTimeout(function(){
			    	 heartFun(true);
				},12000000);//20分钟一次，实际可以和服务器session过期时间保持一致
			}
		},
		error:function(XMLHttpRequest){//遇到错误后，停止心跳
			if(window.localStorage){
				var userInfo = getLocalUserInfo();
				userInfo._loginStatus = false;
				var userInfoStr = JSON.stringify(userInfo); 
				window.localStorage.setItem('userInfo',userInfoStr);
			}
		}
	});
};











